package com.tracesystem.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class WoniuCopyUtil {

    public static <T> T copyOne(Object source, Class<T> clazz) {
        T t = null;
        try {
            t = clazz.newInstance();
            BeanUtils.copyProperties(source, t);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> copyMany(List source, Class<T> clazz) {
        ArrayList<T> list = new ArrayList<>();
        for (Object obj : source) {
            try {
                T t = clazz.newInstance();
                BeanUtils.copyProperties(obj, t);
                list.add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
